package org.demo.szml.perssionservice.api;

import org.demo.szml.api.dto.PermissionBindingReqDTO;
import org.demo.szml.api.dto.PermissionBindingResDTO;
import org.demo.szml.api.dto.PermissionUserAttributionResDTO;
import org.demo.szml.api.response.Response;
import org.demo.szml.api.response.ResponseCode;
import org.demo.szml.api.rpc.IPermissionRPCService;
import org.demo.szml.perssionservice.service.IPermissionService;
import org.demo.szml.perssionservice.service.result.PermissionAttributeHandleResult;
import org.demo.szml.perssionservice.service.result.PermissionBaseResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description: 权限RPC服务提供者
 * @date: 2025/6/15 23:02
 * @version: 1.0
 */
@RestController("/permission-service")
public class PermissionApplication implements IPermissionRPCService {

    private final IPermissionService permissionService;

    public PermissionApplication(IPermissionService permissionService){
        this.permissionService = permissionService;
    }

    @Override
    public Response<PermissionBindingResDTO> bindDefaultRole(@RequestParam("userId") Long userId) {
        PermissionBaseResult permissionBaseResult = permissionService.bindDefaultRole(userId);
        if (!permissionBaseResult.getHandleSuccess()){
            return Response.<PermissionBindingResDTO>builder()
                    .code(ResponseCode.REASON_FAILED).info(permissionBaseResult.getExtInfo()).build();
        }
        return Response.<PermissionBindingResDTO>builder()
                .code(ResponseCode.SUCCESS).data(PermissionBindingResDTO.builder()
                        .bindingResult(true).extInfo(permissionBaseResult.getExtInfo()).build()).build();
    }

    @Override
    public Response<PermissionUserAttributionResDTO> authAttributeUsers(@RequestParam("operatorId") Long operatorId,
                                                                        @RequestParam("page") Integer page,
                                                                        @RequestParam("size") Integer size) {
        PermissionAttributeHandleResult handleResult = permissionService.getAttributeUsers(operatorId, page, size);
        return assemblyAttributionInformation(handleResult);
    }


    @Override
    public Response<PermissionUserAttributionResDTO> authAttributeUser(Long operatorId, Long userId) {
        PermissionAttributeHandleResult handleResult = permissionService.getAttributeRelation(operatorId, userId);
        return assemblyAttributionInformation(handleResult);
    }

    @Override
    public Response<PermissionBindingResDTO> rebindRole(Long userId, Long operatorId, Integer roleId) {
        PermissionBindingResDTO bindingResDTO = permissionService.rebindRole(userId, operatorId, roleId);
        if(!bindingResDTO.getBindingResult()){
            return Response.<PermissionBindingResDTO>builder()
                    .code(ResponseCode.REASON_FAILED).info(bindingResDTO.getExtInfo()).build();
        }
        return Response.<PermissionBindingResDTO>builder().code(ResponseCode.SUCCESS).build();
    }


    private Response<PermissionUserAttributionResDTO> assemblyAttributionInformation(PermissionAttributeHandleResult handleResult) {
        if (!handleResult.getHandleSuccess()){
            return Response.<PermissionUserAttributionResDTO>builder()
                    .code(ResponseCode.REASON_FAILED).info(handleResult.getExtInfo()).build();
        }
        return Response.<PermissionUserAttributionResDTO>builder()
                .code(ResponseCode.SUCCESS).data(PermissionUserAttributionResDTO.builder()
                        .userIds(handleResult.getUserIds()).total(handleResult.getTotal()).build()).build();
    }
}
